package com.namo.cchapi.controller;

import com.baidu.aip.face.AipFace;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.namo.cchapi.config.BaiduAiConfig;
import com.namo.cchapi.entity.FaceEntity;
import com.namo.cchapi.service.FaceService;
import com.namo.cchcommon.api.CommonResult;
import com.namo.cchcommon.api.IErrorCode;
import com.namo.cchcommon.api.ResultCode;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

/**
 * @author WangXin
 * @date 2021/3/31 16:00
 */
@Slf4j
@RestController
@RequestMapping("/baidu")
public class FaceController {

    @Autowired
    BaiduAiConfig baiduAiConfig;

    @Autowired
    FaceService faceService;

    @Autowired
    Gson gson;

    @RequestMapping(value = "/face/add", method = RequestMethod.POST)
    public CommonResult addFace(@RequestParam("face_url") String faceUrl,@RequestParam("user_id") String userId) {

        if (faceUrl==null||userId==null){
            return CommonResult.failed(ResultCode.VALIDATE_FAILED,"缺少参数！");
        }
        try {
            return faceService.addFace(faceUrl,userId);
        }catch (Exception e){
            log.error("添加人脸失败------FaceController");
        }
        return CommonResult.failed(ResultCode.FAILED);
    }

    @RequestMapping(value = "/face/match",method = RequestMethod.POST)
    public CommonResult matchFace(@RequestBody String req){
        JsonObject jsonObject = gson.fromJson(req,JsonObject.class);
        Boolean result;
        try {
             result = faceService.matchFace(jsonObject.get("face1").getAsString(), jsonObject.get("face2").getAsString());
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
        if (result == false){
            return CommonResult.success(1,"人脸不匹配");
        }
        return CommonResult.success(0,"识别通过");
    }
}
